package com.ricky.base.bean;

import com.ricky.base.listener.TerminalListener;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * @author Ricky
 * @date 2020/11/11
 */
public class StreamGobbler extends Thread {
    InputStream is;

    String type;
    TerminalListener terminalListener;

    public StreamGobbler(InputStream is, String type) {
        this.is = is;
        this.type = type;
    }

    public StreamGobbler(InputStream is, TerminalListener terminalListener, String type) {
        this.is = is;
        this.type = type;
        this.terminalListener = terminalListener;
    }

    @Override
    public void run() {
        try {
            InputStreamReader isr = new InputStreamReader(is, "GBK");
            BufferedReader br = new BufferedReader(isr);
            String line;
            while ((line = br.readLine()) != null) {
                if (terminalListener == null) {
                    System.out.println(type + "\t" + ">>>" + "\t" + line);
                } else {
                    terminalListener.output(line);
                }
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
}
